
// 使用redux 定义store 仓库
// 01:  引入 创建STORE 的方法
import { legacy_createStore as createStore } from 'redux';

// 引入持久化配置方法
import { persistReducer, persistStore } from "redux-persist";

import storage from "redux-persist/lib/storage";
//02: 定义初始的state

const defaultState = {
    acl: [],
    jwt: ''
};


const persitConfig = {
    key: "root",
    storage: storage,
    // 如果不想将部分state持久化，可以将其放入黑名单(blacklist)中.黑名单是设置
    //   blacklist: ['ll']
}
//03: 创建reducer 函数 用来修改redux 中的数据
const reducer = (state = defaultState, actions) => {
    // 修改菜单数组
    if (actions.type == 'setAcl') {
        return {
            ...state,
            acl: actions.payload
        }
    }
    // 修改token 
    if (actions.type == 'setToken') {
        return {
            ...state,
            jwt: actions.payload
        }
    }

    return state
}

// 04: 创建store 仓库
//const store = createStore(reducer, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());

//创建持久化的配置persist的信息
const persist_reducers = persistReducer(persitConfig, reducer);

const store = createStore(persist_reducers, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());

const persistor = persistStore(store);
// 05: 导出仓库
export { store, persistor }

